package com.yc.game.controller;

import com.github.pagehelper.PageInfo;
import com.yc.game.pojo.Category;
import com.yc.game.pojo.Food;
import com.yc.game.pojo.Recipes;
import com.yc.game.service.CategoryService;
import com.yc.game.service.FoodService;
import com.yc.game.service.RecipesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
public class RecipesController {
    @Autowired
    private RecipesService recipesService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private FoodService foodService;

    /**
     * 获取详情
     */
    @RequestMapping("recipes/recipesDetail")
    public String recipesDetail(@RequestParam("recipesId") Integer recipesId, ModelMap modelMap) {
        Recipes recipes = recipesService.getById(recipesId);
        modelMap.addAttribute("recipes", recipes);
        List<String> strings = spiltString(recipes.getContent());
        modelMap.addAttribute("strings", strings);
        // 获取最新的三条
        List<Food> foods = foodService.getByUpdateTime();
        modelMap.addAttribute("foods", foods);
        return "recipesDetail";
    }

    /**
     * 新闻列表
     */
    @RequestMapping("page/recipes")
    public String recipesPage(ModelMap modelMap,
                              @RequestParam(value = "pageNum", defaultValue = "2") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "1") Integer pageSize) {
        PageInfo<Recipes> recipes = recipesService.getAll(pageNum, pageSize);
        modelMap.addAttribute("recipes", recipes);
        List<Category> categories = categoryService.getAll();
        modelMap.addAttribute("categories", categories);
        // 获取最新的三条
        List<Food> foods = foodService.getByUpdateTime();
        modelMap.addAttribute("foods", foods);
        return "recipes";
    }

    /**
     * 分割工具
     */
    private List<String> spiltString(String str) {
        String[] strings = str.split(";");
        List<String> stringList = new ArrayList<>();
        for (String string : strings) {
            stringList.add(string);
        }
        return stringList;
    }
}
